package com.cxyd.modules.api.controller;

import com.cxyd.common.annotation.LogOperation;
import com.cxyd.common.utils.Result;
import com.cxyd.common.constant.Constant;
import com.cxyd.common.utils.ApiResult;
import com.cxyd.common.utils.ProcResult;
import com.cxyd.modules.api.dto.CallbackDTO;
import com.cxyd.modules.api.dto.StockDetailDTO;
import com.cxyd.modules.api.dto.StockParamsDTO;
import com.cxyd.modules.api.report.*;
import com.cxyd.modules.api.service.CallbackService;
import com.cxyd.modules.wms.dto.InterfaceMesReqDTO;
import com.cxyd.modules.wms.service.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class ReceiveApiController {
    private final CallbackService receiveWcsService;
    private final OrderService orderService;
    private final StockService stockService;
    private final OrderDetailService orderDetailService;
    private final LocateService locateService;
    private final PurchaseDetailService purchaseDetailService;
    private final OrderReelService orderReelService;

    public ReceiveApiController(CallbackService receiveWcsService, OrderService orderService, StockService stockService, OrderDetailService orderDetailService, LocateService locateService, PurchaseDetailService purchaseDetailService, OrderReelService orderReelService) {
        this.receiveWcsService = receiveWcsService;
        this.orderService = orderService;
        this.stockService = stockService;
        this.orderDetailService = orderDetailService;
        this.locateService = locateService;
        this.purchaseDetailService = purchaseDetailService;
        this.orderReelService = orderReelService;
    }

    /**
     * 连接测试
     */
    @GetMapping("ping")
    public ApiResult ping() {
        return new ApiResult().success("当前时间:" + LocalTime.now());
    }


    @PostMapping("wcsCallback")
    public ApiResult wcsCallBack(@RequestBody CallbackDTO dto) {
        //数据校验
        final String requestPk = dto.getRequestPk();
        if (requestPk.length() == 0) {
            return new ApiResult().feedBack("requestPk不能为空!", "404");
        }
        if (dto.getCurPos().length() == 0) {
            return new ApiResult().feedBack("当前位置不能为空!", requestPk);
        }

        final Map<String, Object> params = new HashMap<>();
        params.put("requestPk", requestPk);

        final CallbackDTO record = receiveWcsService.getOneByPk(params);
        if (record != null) {
            return new ApiResult().feedBack("记录重复上报!", requestPk);
        }
        //数据记录（逻辑处理交由扫描处理）
        //receiveWcsService.save(dto);
        params.put("userId", Constant.ClientCode.WCS.getId());
        params.put("contNo", dto.getContNo());
        params.put("noticeType", dto.getNoticeType());
        params.put("curPos", dto.getCurPos());
        //数据处理
        final ProcResult result = receiveWcsService.callbackHandle(params);
        //数据结果，再保存
        dto.setCode(result.getCode());
        dto.setResult(result.getMsg());
        receiveWcsService.save(dto);
        //反馈成功
        return new ApiResult().feedBack(result.getCode(), result.getMsg(), requestPk);
    }

    @PostMapping("import")
    @LogOperation("导入出库单")
    // @RequiresPermissions("wms:order-detail:import")
    public Result importOrderDetailList(@RequestParam("file") MultipartFile file) throws Exception{

        return orderDetailService.procDetailImport(file);
    }

    @PostMapping("stock-query")
    public Result stockQuery(@RequestBody StockParamsDTO dto) {
        if (dto == null) {
            return new Result<>().error(500, "请求体异常!系统无法识别");
        }
        if (!"K117".equals(dto.getLGNUM())) {

            return new Result<>().error(500, "仓库编号[" + dto.getLGNUM() + "]无法识别,正确编号为[K117]");
        }
        Map<String, Object> params = new HashMap<>();
        params.put("matnr", dto.getMATNR());
        List<StockDetailDTO> page = stockService.stockQuery(params);
        return new Result().ok(page);
    }

    @PostMapping("order-import")
    public Result orderImport(@RequestBody InterfaceMesReqDTO dto) {
        if (dto == null) {
            return new Result().error("领料单无法解析");
        }
        if (StringUtils.isBlank(dto.getZllbh())) {
            return new Result().error("领料单号不能为空");
        }
        return orderService.importOrder(dto.getZllbh(), dto.getData());
    }

    //报表内容
    //总储位数及已使用数
    @GetMapping("locateCount/{dvcNo}")
    public LocateCount locateCount(@PathVariable("dvcNo") String dvcNo) {
        //数据校验
        return locateService.getLocateCount(dvcNo);

    }

    @GetMapping("contCount/{dvcNo}")
    public LocateCount contCount(@PathVariable("dvcNo") String dvcNo) {
        //数据校验
        return locateService.getContCount(dvcNo);

    }
    @GetMapping("inCount")
    public Result<List<InoutCount>> inCount() {
        List<InoutCount> dataIn = purchaseDetailService.getIncount();
        //数据校验
        return new Result<List<InoutCount>>().ok(dataIn);

    }
    @GetMapping("outCount")
    public Result<List<InoutCount>> outCount() {
        //数据校验
        List<InoutCount> dataOut = orderReelService.getOutcount();
        //数据校验
        return new Result<List<InoutCount>>().ok(dataOut);

    }

    @GetMapping("stockDay")
    public StockDayRes stockDay() {
        //数据校验
        return stockService.getstockDays();

    }

    @GetMapping("stockDay/{dvcNo}")
    public StockDayRes stockDayDvcNo(@PathVariable("dvcNo") String dvcNo) {
        //数据校验
        return stockService.getstockDaysDvcNo(dvcNo);
    }

    @GetMapping("restCount")
    public RestCount restCount() {
        //数据校验
        return stockService.getRestCount();

    }

    @GetMapping("orderCount")
    public Result<List<OrderCount>> orderCount() {
        //数据校验
        //数据校验
        List<OrderCount> dataOut = orderReelService.getOrderCount();
        //数据校验
        return new Result<List<OrderCount>>().ok(dataOut);

    }
}
